<?php
/**
 * RAI配置
 *
 * @version $Id: RaiConfig.php 312 2008-04-23 16:29:38Z iwind $
 * @package net.4kiki.rai
 * @since 0.0.3
 */
class RaiConfig {
	private static $formatMap = array(
		"JSON" => array(
					"class" => "net.4kiki.rai.format.RaiJsonFormat",
					"regexp" => "^\{"
					),
		"XmlRpc" => array(
					"class" => "net.4kiki.rai.format.RaiXmlRpcFormat",
					"regexp" => "^<\\?xml"
					),
		"bencode" => array(
					"class" => "net.4kiki.rai.format.RaiBencodeFormat",
					"regexp" => "\\w+"
					)
	);
	
	/**
	 * 根据输出的字符串自动检测格式
	 *
	 * 当前版本中仅提供JSON, XmlRpc, bencode三种格式
	 * 
	 * @return RaiFormat
	 * @since 0.0.3
	 */	
	public static function detectFormat($inputString) {
		foreach (self::$formatMap as $name=>$format) {
			if (preg_match("/" . $format["regexp"] . "/isU", $inputString)) {
				return IObjectFactory::getSingleObject($format["class"]);
			}
		}
		throw new RaiException("unknown data format");
	}
}

?>